//
//  StatusPictureView.swift
//  VVbo
//
//  Created by Mr.Run on 2017/8/29.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit

class StatusPictureView: UIView {
    
    var viewModel:StatusViewModel? {
        didSet{
            calcViewSize()
            
            urls = viewModel?.picURLs
            
        }
    }
    
    fileprivate func calcViewSize() {
        
        if viewModel?.picURLs?.count == 1 {
            let viewSize = viewModel?.pictureViewSize ?? CGSize()
            let v = subviews[0]
            v.frame = CGRect(x: 0, y: StatusPictureViewOutterMargin, width: viewSize.width, height: viewSize.height - StatusPictureViewOutterMargin)
        }else{
            let v = subviews[0]
            v.frame = CGRect(x: 0, y: StatusPictureViewOutterMargin, width: StatusPictureItemWidth, height: StatusPictureItemWidth)
        }
        
        heightCons.constant = viewModel?.pictureViewSize.height ?? 0
    }
    
    private var urls: [StatusPicture]?{
        didSet{
            
            for v in subviews {
                v.isHidden = true
            }
            var index = 0
            
            for url in urls ?? [] {
                
                let iv = subviews[index] as! UIImageView
                
                if index == 1 && urls?.count == 4{
                    index += 1
                }
                
                iv.vv_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                
                iv.subviews[0].isHidden = (((url.thumbnail_pic ?? "") as NSString).pathExtension.lowercased() == "gif")
                
                iv.isHidden = false
                index += 1
            }
            
        }
    }
    
    
    @IBOutlet weak var heightCons: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        setupUI()
        
    }
    
    @objc fileprivate func tapImageView(tap: UITapGestureRecognizer) {
        
        guard let iv = tap.view,
            let picURLs = viewModel?.picURLs
            else {
                return
        }
        
        var selectedIndex = iv.tag
        
        if picURLs.count == 4 && selectedIndex > 1 {
            selectedIndex -= 1
        }
        
        let urls = (picURLs as NSArray).value(forKey: "largePic") as! [String]
        
        var imageViewList = [UIImageView]()
        
        for iv in subviews as! [UIImageView] {
            if !iv.isHidden {
                imageViewList.append(iv)
            }
        }
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: StatusCellBrowserPhotoNotification), object: self, userInfo: [StatusCellBrowserPhotoURLsKey: urls,StatusCellBrowserPhotoSelectedIndexKey: selectedIndex,                      StatusCellBrowserPhotoImageViewsKey: imageViewList])
    }
    
}

extension StatusPictureView {
    
    fileprivate func setupUI() {
        backgroundColor = superview?.backgroundColor
        clipsToBounds = true
        let rect = CGRect(x: 0, y: StatusPictureViewOutterMargin, width: StatusPictureItemWidth, height: StatusPictureItemWidth)
        
        let count = 3
        for i in 0..<count*count {
            let iv = UIImageView()
            iv.clipsToBounds = true
            iv.contentMode = .scaleAspectFill
            let row = CGFloat(i/count)
            let col = CGFloat(i%count)
            let xOffset = col*(StatusPictureItemWidth + StatusPictureViewInnerMargin)
            let yOffset = row*(StatusPictureItemWidth + StatusPictureViewInnerMargin)
            
            iv.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            addSubview(iv)
            iv.isUserInteractionEnabled = true
            
            let tap = UITapGestureRecognizer(target: self, action: #selector(tapImageView))
            iv.addGestureRecognizer(tap)
            iv.tag = i
            addGifView(iv: iv)
        }
    }
    
    private func addGifView(iv:UIImageView) {
        let gifImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
        iv.addSubview(gifImageView)
        
        gifImageView.translatesAutoresizingMaskIntoConstraints = false
        iv.addConstraint(NSLayoutConstraint(item: gifImageView, attribute: .right, relatedBy: .equal, toItem: iv, attribute: .right, multiplier: 1.0, constant: 0))
        iv.addConstraint(NSLayoutConstraint(item: gifImageView, attribute: .bottom, relatedBy: .equal, toItem: iv, attribute: .bottom, multiplier: 1.0, constant: 0))
        
    }
    
}



